package first.endtoend;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import first.endtoend.adapters.SupplyAdapter;
import first.endtoend.helpers.DialogHelper;

/**
 * 
 * This activity is used to process only one product to beneficiary
 *
 */
public class SupplyActivity extends MyActivity{

	//Variables
	EditText editText;
	Float quantityWished;
	Button validateBtn, backBtn;
	ListView listview;
	SupplyAdapter adapter;	



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//set the layout to the activity
		setContentView(R.layout.activity_supply);


		validateBtn = (Button) findViewById(R.id.validBtnSupply);


		listview = (ListView)findViewById(R.id.listView1);

		adapter = new SupplyAdapter(ListBeneficiariesActivity.portfolio.getProductsSelected(), SupplyActivity.this);
		listview.setAdapter(adapter);

		validateBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				int verif = thereIsEmptyEditText();
				int value = invalidValues();
				if(verif != 1 && value != -1){
					Intent intent = new Intent(SupplyActivity.this, ConfirmationSupplyActivity.class);
					startActivity(intent);
					finish();

				}
				else{
					AlertDialog.Builder alert = new AlertDialog.Builder(SupplyActivity.this);
					DialogHelper.showErrorDialog(alert, getString(R.string.information), getString(R.string.emptyFields));
				}
				
//				int value = invalidValues();
//				if(value != -1){
//					Intent intent = new Intent(SupplyActivity.this, ConfirmationSupplyActivity.class);
//					startActivity(intent);
//					finish();
//				}else{
//					AlertDialog.Builder alert = new AlertDialog.Builder(SupplyActivity.this);
//					DialogHelper.showErrorDialog(alert, getString(R.string.warning), getString(R.string.invalidValue));
//				}
			}
		});
	}

	//method to check if there is one editText empty in the layout
	public int thereIsEmptyEditText(){
		int result = 0;
		for(int i=0; i < listview.getChildCount(); i++){
			LinearLayout itemLayout = (LinearLayout)listview.getChildAt(i);
			editText = (EditText)itemLayout.findViewById(R.id.quantityWished);
			if(editText.getText().toString().equals("")){			
				return 1;
			}}

		return result;		

	}
	
	/**
	 * method to check if the agent ask 0 wish quantity or no  
	 * */
	public int invalidValues(){
		int value=0;
		for (int index=0; index < listview.getChildCount(); index ++){
			LinearLayout itemLayout = (LinearLayout) listview.getChildAt(index);
			editText= (EditText) itemLayout.findViewById(R.id.quantityWished);
			int intValue = Integer.parseInt(editText.getText().toString());
			if(intValue == 0){
				return -1; 
			}
		}
		return value;
		
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(SupplyActivity.this, PortfolioActivity.class);
		startActivity(intent);
		finish();
	}
}
